package com.elitedatai.mchtest.manage.controller;

import com.dji.sdk.common.HttpResultResponse;
import com.elitedatai.mchtest.common.model.CustomClaim;
import com.elitedatai.mchtest.manage.model.dto.WorkspaceDTO;
import com.elitedatai.mchtest.manage.service.IWorkSpaceService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

import static com.elitedatai.mchtest.component.AuthInterceptor.TOKEN_CLAIM;


/**
 * 工作空间控制器，处理工作空间相关的HTTP请求
 */
@RestController
@RequestMapping("${url.manage.prefix}${url.manage.version}/workspaces")
public class WorkspaceController {

    @Autowired
    private IWorkSpaceService workspaceService;

    /**
     * 获取当前用户所在的工作空间信息
     * @param request HTTP请求对象，用于获取用户凭证信息
     * @return 工作空间信息响应
     */
    @GetMapping("/current")
    public HttpResultResponse getCurrentWorkspace(HttpServletRequest request) {
        CustomClaim customClaim = (CustomClaim)request.getAttribute(TOKEN_CLAIM);
        Optional<WorkspaceDTO> workspaceOpt = workspaceService.getWorkspaceByWorkspaceId(customClaim.getWorkspaceId());

        return workspaceOpt.isEmpty() ? HttpResultResponse.error() : HttpResultResponse.success(workspaceOpt.get());
    }
}